package mypackage;

import java.util.Vector;

import mypackage.dao.ItemDataProvider;
import mypackage.dao.ListDao;
import mypackage.dao.ListItemDao;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.KeywordFilterField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.menu.SubMenu;

public class CheckboxListScreen extends MainScreen {
	KeywordFilterField keywordFilterField;
	ItemDataProvider itemDataProvider;
	ListDao listDao;

	public CheckboxListScreen(ListDao listDao) {
		setTitle(listDao.listName);
		this.listDao = listDao;
		itemDataProvider = new ItemDataProvider(listDao.listItems);

		keywordFilterField = new KeywordFilterField() {
			protected boolean navigationClick(int status, int time) {
				// TODO open the details screen here
				if (keywordFilterField.getSize() > 0)
					editElementItem.run();
				else
					addElementItem.run();
				return true;
			};

			protected boolean keyChar(char key, int status, int time) {
				if (key == Characters.BACKSPACE
						&& (keywordFilterField.getKeyword() == null || keywordFilterField
								.getKeyword().equals("")))
					deleteCurrentMenuItem.run();
				else if (key == Characters.SPACE
						&& (keywordFilterField.getKeyword() == null || keywordFilterField
								.getKeyword().equals(""))) {
					ListItemDao listItemDao = (ListItemDao) keywordFilterField
							.getSelectedElement();
					listItemDao.done = !listItemDao.done;
					refrsh();

				}
				return super.keyChar(key, status, time);
			};
		};
		keywordFilterField.setSourceList(itemDataProvider, itemDataProvider);
		keywordFilterField
				.setEmptyString("* Add this one *", DrawStyle.HCENTER);
		keywordFilterField.setCallback(listFieldCallback);

		initMenus();

		add(keywordFilterField.getKeywordField());
		add(keywordFilterField);
		refrsh();
		addMenuItem(addElementItem);
		addMenuItem(editElementItem);

	}

	void addElementToList(String country) {
		itemDataProvider.addItem(country);
		refrsh();
	}

	public boolean onClose() {
		listDao.listItems = itemDataProvider.getVector();
		return super.onClose();
	}

	public void refrsh() {
		Vector items = itemDataProvider.getVector();
		int focusRow = keywordFilterField.getSelectedIndex();
		if (focusRow >= items.size())
			focusRow = items.size() - 1;
		keywordFilterField.updateList();
		keywordFilterField.setSelectedIndex(focusRow);
	}

	void editElementInList(String str) {
		itemDataProvider.updateItem(keywordFilterField.getSelectedElement(),
				new ListItemDao(str, 0 + "", false));
		refrsh();
	}

	MenuItem delteAllMenuItem, deleteCurrentMenuItem, deleteMarkedItem,
			deleteUnmarkedItem, addElementItem, editElementItem;

	private void initMenus() {
		delteAllMenuItem = new MenuItem("All", 0, 0) {
			public void run() {
				if (Dialog.ask(Dialog.D_YES_NO, "Are you sure ?") == Dialog.YES) {
					itemDataProvider.deleteAll();
					refrsh();
				}
			};
		};

		deleteCurrentMenuItem = new MenuItem("Current", 0, 0) {
			public void run() {
				if (Dialog.ask(Dialog.D_YES_NO, "Are you sure ?") == Dialog.YES) {
					if (keywordFilterField.getSize() > 0) {
						itemDataProvider.deleteItem(keywordFilterField
								.getSelectedElement());
						refrsh();
					}
				}
			};

		};

		deleteMarkedItem = new MenuItem("Marked", 0, 0) {
			public void run() {
				if (Dialog.ask(Dialog.D_YES_NO, "Are you sure ?") == Dialog.YES) {
					// TODO deleteMarkedItems(true);
					itemDataProvider.delete(true);
					refrsh();
				}

			};
		};
		deleteUnmarkedItem = new MenuItem("Unmarked", 0, 0) {
			public void run() {
				if (Dialog.ask(Dialog.D_YES_NO, "Are you sure ?") == Dialog.YES) {
					itemDataProvider.delete(false);
					refrsh();
				}
			};
		};

		addElementItem = new MenuItem("New", 0, 0) {

			public void run() {
				String[] selections = { "Add", "Cancel" };

				final Dialog addDialog = new Dialog("Add item", selections,
						null, 0, null);
				EditField inputField = new EditField("Name: ",
						keywordFilterField.getKeyword()) {
					protected boolean keyChar(char key, int status, int time) {
						if (key == Characters.ENTER) {
							addDialog.select(0);
							return true;
						} else
							return super.keyChar(key, status, time);
					};
				};
				addDialog.add(inputField);

				if (addDialog.doModal() == 0) {
					keywordFilterField.setKeyword(null);
					addElementToList(inputField.getText());
				}
			}
		};

		editElementItem = new MenuItem("Edit", 0, 0) {
			public void run() {
				if (keywordFilterField.getSize() == 0)
					return;
				ListItemDao item = (ListItemDao) keywordFilterField
						.getSelectedElement();
				String[] selections = { "Done", "Cancel" };
				final Dialog addDialog = new Dialog("Edit item", selections,
						null, 0, null);
				EditField inputField = new EditField("Name: ", item.name) {
					protected boolean keyChar(char key, int status, int time) {
						if (key == Characters.ENTER) {
							addDialog.select(0);
							return true;
						} else
							return super.keyChar(key, status, time);
					};
				};
				addDialog.add(inputField);

				if (addDialog.doModal() == 0) {
					editElementInList(inputField.getText());
				}
			}
		};
	}

	protected void makeMenu(net.rim.device.api.ui.component.Menu menu,
			int instance) {

		SubMenu subMenu = new SubMenu(new MenuItem[] { deleteCurrentMenuItem,
				deleteMarkedItem, deleteUnmarkedItem, delteAllMenuItem },
				"Delete", 100, 0);
		menu.add(subMenu);
		super.makeMenu(menu, instance);
	};

	ListFieldCallback listFieldCallback = new ListFieldCallback() {

		public int indexOfList(ListField listField, String prefix, int start) {
			return itemDataProvider.getVector().indexOf(prefix, start);

		}

		public int getPreferredWidth(ListField listField) {
			return Display.getWidth();
		}

		public Object get(ListField listField, int index) {
			// TODO Auto-generated method stub
			return itemDataProvider.getVector().elementAt(index);
		}

		public void drawListRow(ListField listField, Graphics graphics,
				int index, int y, int width) {
			if (index < itemDataProvider.size()) {
				ListItemDao listItemDao = (ListItemDao) itemDataProvider
						.getVector().elementAt(index);
				String str = (listItemDao.done ? Characters.BALLOT_BOX_WITH_CHECK
						: Characters.BALLOT_BOX)
						+ " " + listItemDao.name;
				graphics.drawText(str, 10, y, DrawStyle.ELLIPSIS, width);
			}
		}
	};

}
